#include "BaseWidget.h"
#include <QStylePainter>
#include <QWindow>
#include <QVBoxLayout>
#include <QHBoxLayout>
#include <QGroupBox>
#include <QLabel>
#if WIN32
#include <Windows.h>
#include <winuser.h>
#endif

BaseWidget::BaseWidget(QWidget* parent)
    : QWidget(parent)
{
	setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
}

BaseWidget::~BaseWidget()
{
	stopIntervalTimer();
	stopTimer();
}

void BaseWidget::setupUi(QStringList strList)
{
	QVBoxLayout* mainLayout = new QVBoxLayout(this);

	QString strGroupBoxStyleSheet = "QGroupBox {\
			border:1px solid rgb(23, 36, 34);\
			margin-top:2.5ex;\
		}\
		QGroupBox::title {\
			color: rgb(255,235,200);\
			subcontrol-origin: margin;\
			subcontrol - position: top left;\
			left:10px;\
			top:0px;\
			padding: 0px;\
		}\
	";

	QGroupBox* reqGroupBox = new QGroupBox(tr("请求参数"));
	reqGroupBox->setStyleSheet(strGroupBoxStyleSheet);

	QVBoxLayout* vLayout = new QVBoxLayout(reqGroupBox);
	QHBoxLayout* hLayout1 = new QHBoxLayout;
	QHBoxLayout* hLayout2 = new QHBoxLayout;
	QHBoxLayout* hLayout3 = new QHBoxLayout;
	QHBoxLayout* hLayout4 = new QHBoxLayout;
	QHBoxLayout* hLayoutReq = new QHBoxLayout;

	// 第一行
	QLabel* label1 = new QLabel(tr("请求功能号"), this);
	hLayout1->addWidget(label1);

	checkCombo = new qb::SCheckComboBox(strList, this);
	hLayout1->addWidget(checkCombo, 1);
	vLayout->addLayout(hLayout1);

	// 第二行
	QLabel* label2 = new QLabel(tr("请求次数"), this);
	hLayout2->addWidget(label2);

	editTimes = new SLineEdit("", this);
	editTimes->setFixedWidth(100);
	hLayout2->addWidget(editTimes, 1, Qt::AlignLeft);
	hLayout2->setSpacing(8);
	vLayout->addLayout(hLayout2);

	// 第三行
	QLabel* label3 = new QLabel(tr("请求超时"), this);
	hLayout3->addWidget(label3);

	editTimeout = new SLineEdit("", this);
	editTimeout->setFixedWidth(100);
	hLayout3->addWidget(editTimeout, 0, Qt::AlignLeft);

	QLabel* labelSec = new QLabel(tr("秒"), this);
	hLayout3->addWidget(labelSec, 1, Qt::AlignLeft);
	hLayout3->setSpacing(8);
	vLayout->addLayout(hLayout3);

	// 第四行
	QLabel* label4 = new QLabel(tr("请求间隔"), this);
	hLayout4->addWidget(label4);

	editInterval = new SLineEdit("", this);
	editInterval->setFixedWidth(100);
	hLayout4->addWidget(editInterval, 0, Qt::AlignLeft);

	QLabel* labelMSec = new QLabel(tr("毫秒"), this);
	hLayout4->addWidget(labelMSec, 1, Qt::AlignLeft);
	hLayout4->setSpacing(8);
	vLayout->addLayout(hLayout4);

	btnRequest = new QPushButton(tr("请 求"), this);
	btnRequest->setFixedSize(QSize(300, 40));
	hLayoutReq->addWidget(btnRequest, 0, Qt::AlignCenter|Qt::AlignBottom);
	vLayout->addLayout(hLayoutReq, 1);
	reqGroupBox->setLayout(vLayout);

	QGroupBox* rspGroupBox = new QGroupBox(tr("响应日志"));
	rspGroupBox->setStyleSheet(strGroupBoxStyleSheet);

	QVBoxLayout* vLayout2 = new QVBoxLayout(rspGroupBox);
	editor = new QTextEdit();
	editor->setStyleSheet("QTextEdit{background-color:rgb(18,26,25);}");
	editor->setReadOnly(true);
	editor->document()->setMaximumBlockCount(1000);
	vLayout2->addWidget(editor);

	btnClear = new QPushButton(tr("清空日志"), this);
	btnClear->setFixedSize(QSize(80, 30));
	vLayout2->addWidget(btnClear, 0, Qt::AlignRight);
	rspGroupBox->setLayout(vLayout2);

	mainLayout->addWidget(reqGroupBox);
	mainLayout->addWidget(rspGroupBox);
	mainLayout->setStretchFactor(reqGroupBox, 1);
	mainLayout->setStretchFactor(rspGroupBox, 1);

	this->setLayout(mainLayout);

	mTimer = new QTimer(this);
	mIntervalTimer = new QTimer(this);
	connect(mTimer, SIGNAL(timeout()), this, SLOT(onTimer()), Qt::UniqueConnection);
	connect(mIntervalTimer, SIGNAL(timeout()), this, SLOT(onIntervalTimer()), Qt::UniqueConnection);
}

void BaseWidget::setTimeout(int sec)
{
	QB_CHECK_RETURN_VOID1(mTimer);
	mTimer->setInterval(sec * 1000);
}

void BaseWidget::setInterval(int msec)
{
	QB_CHECK_RETURN_VOID1(mIntervalTimer);
	mIntervalTimer->setInterval(msec);
}

void BaseWidget::startTimer()
{
	QB_CHECK_RETURN_VOID1(mTimer);
	mTimer->start();
}

void BaseWidget::stopTimer()
{
	QB_CHECK_RETURN_VOID1(mTimer);
	mTimer->stop();
}

void BaseWidget::startIntervalTimer()
{
	QB_CHECK_RETURN_VOID1(mIntervalTimer);
	mIntervalTimer->start();
}

void BaseWidget::stopIntervalTimer()
{
	QB_CHECK_RETURN_VOID1(mIntervalTimer);
	mIntervalTimer->stop();
}

void BaseWidget::onTimer()
{
	onTimeout();
}

void BaseWidget::onIntervalTimer()
{
	onIntervalTimeout();
}

void BaseWidget::paintEvent(QPaintEvent* event)
{
    QStylePainter painter(this);
}

void BaseWidget::resizeEvent(QResizeEvent* event)
{

}
